
async function fetchGiteeData() {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.open('GET', 'https://api.zengjun.tech/mind/api/get', true);
        xhr.onload = function () {
            if (xhr.status === 200) {
                const data = JSON.parse(xhr.responseText);
                console.log('【Success fetching data】',data);
                resolve(data);
            } else {
                console.error('Error fetching data:', xhr.statusText);
                reject(new Error('Error fetching data'));
            }
        };
        xhr.onerror = function () {
            console.error('Network error occurred.');
            reject(new Error('Error fetching data'));
        };
        xhr.send();
    });
}

/**
 * 保存接口
 * @param mapMindData
 * @returns {Promise<void>}
 */
async function saveGiteeData(mapMindData){
    return new Promise((resolve,reject)=>{
        var formData = new FormData();
        const blob = new Blob([mapMindData], { type: 'application/json' });
        var file = new File([blob], 'convertedFile.json', { type: 'application/json' });
        formData.append('file', file);
        var xhr = new XMLHttpRequest();
        xhr.open('POST', 'https://api.zengjun.tech/mind/api/add', true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                console.log(xhr.responseText);
                if(xhr.responseText === '200') {
                    resolve();
                }else{
                    reject();
                }
            }
        };
        xhr.onerror = function () {
            console.error('Network error occurred.');
            reject(new Error('Error fetching data'));
        };
        xhr.send(formData);
    })
}

// 获取思维导图数据
const giteeData = async () => {
    try {
        const jsonData = await fetchGiteeData();
        console.log(8888,jsonData)
        return jsonData;
    } catch (error) {
        console.error('Error fetching data:', error);
    }
};

export { fetchGiteeData,giteeData,saveGiteeData};
